package singleUseDeprecated;

import com.google.gson.JsonElement;

public class GetAsCharacterExample2_8_7 {

    /**
     * 安全封装的 getAsCharacter 方法。
     */
    public char getAsCharacter(JsonElement element) {
        if (element == null) {
            throw new IllegalArgumentException("JsonElement cannot be null.");
        }

        try {
            // 调用原始的 getAsCharacter 方法
            return element.getAsCharacter();
        } catch (IllegalStateException | ClassCastException e) {
            throw new IllegalArgumentException("Failed to extract character from JsonElement.", e);
        }
    }
    //替换API
    public char getAsCharacter2(JsonElement element) {
        if (element == null) {
            throw new IllegalArgumentException("JsonElement cannot be null.");
        }

        try {
            // 调用原始的 getAsCharacter 方法
            return element.getAsString().charAt(0);
        } catch (IllegalStateException | ClassCastException e) {
            throw new IllegalArgumentException("Failed to extract character from JsonElement.", e);
        }
    }
}
